Shader "post/transDoor"
{
    Properties
    {
        _NoiseTex ("NoiseTex", 2D) = "black" {}
        _StrokeTex ("StrokeTex", 2D) = "black" {}
        _LightTex ("LightTex", 2D) = "black" {}
        _EdgeTex ("EdgeTex", 2D) = "black" {}
        _NoiseSize ("NoiseSize", Float) = 0.1
        _BlurSize ("BlurSize", Range(1,50)) = 20
        _IntersectScale ("IntersectScale", Range(0,10)) = 10 //Range(50,0.1
    }

    SubShader{
        Tags { "Queue" = "Transparent" "RenderType" = "Opaque" }
        GrabPass{"_BackgroudTex"} //水池底部
        //ZTest Always


        CGINCLUDE
        #include "UnityCG.cginc"

        sampler2D _BackgroudTex;
        sampler2D _NoiseTex;
        sampler2D _StrokeTex;
        sampler2D _LightTex;
        sampler2D _EdgeTex;

        sampler2D _CameraDepthTexture;

        //UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
        
        float4 _BackgroudTex_TexelSize;
        float _NoiseSize;
        float _BlurSize;
        float _IntersectScale;

        struct v2fVert { 
			float4 pos : SV_POSITION;
            half2 uv : TEXCOORD0;
            float4 scrPos :TEXCOORD1;
            float4 worldPos : TEXCOORD2;
            float3 normal : NORMAL;
		};
		  
		v2fVert vertGrabBlur(appdata_full v) {
			v2fVert o;
			o.pos = UnityObjectToClipPos(v.vertex);
            o.uv = v.texcoord;
			o.scrPos = ComputeGrabScreenPos(o.pos);
            o.normal = mul(UNITY_MATRIX_IT_MV, v.normal);
            o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyzw /1.0; 
					 
			return o;
		}
		
		fixed4 fragGrabBlur(v2fVert i) : SV_Target {

            //--------- 相交处 ------------
            //利用屏幕坐标采样深度图，并对深度解码得到深度图中的线性深度（摄像机空间）
            float depthTexDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos))); 
            float screenDepth = i.scrPos.z / i.scrPos.w; //经过齐次除法，得到屏幕深度值
            screenDepth = (UNITY_NEAR_CLIP_VALUE >= 0) ? screenDepth : screenDepth * 0.5 + 0.5; //根据平台不同，修正屏幕深度值
            screenDepth = LinearEyeDepth(screenDepth); //得到线性的屏幕深度
            //得到深度差值，并用参数控制大小
            float distanceRele = 1.0f - clamp((depthTexDepth - screenDepth) * _IntersectScale, 0, 1) ; 
            distanceRele = abs(depthTexDepth)/25 ; 
            // distanceRele = (1-saturate(abs(depthTexDepth - screenDepth) * _IntersectScale)) ; 
            //distanceRele = screenDepth;

            //--------- 模糊 ------------
			// float weight[9] = 
            //     {
            //         0.0947, 0.1183, 0.0947,
            //         0.1183, 0.1477, 0.1183,
            //         0.0947, 0.1183, 0.0947,
            //     };

                float weight[49] = 
                {
                   0.00000067f,  0.00002292f,  0.00019117f,  0.00038771f,  0.00019117f,  0.00002292f,  0.00000067f,
                    0.00002292f,  0.00078634f,  0.00655965f,  0.01330373f,  0.00655965f,  0.00078633f,  0.00002292f,
                    0.00019117f,  0.00655965f,  0.05472157f,  0.11098164f,  0.05472157f,  0.00655965f,  0.00019117f,
                    0.00038771f,  0.01330373f,  0.11098164f,  0.22508352f,  0.11098164f,  0.01330373f,  0.00038771f,
                    0.00019117f,  0.00655965f,  0.05472157f,  0.11098164f,  0.05472157f,  0.00655965f,  0.00019117f,
                    0.00002292f,  0.00078633f,  0.00655965f,  0.01330373f,  0.00655965f,  0.00078633f,  0.00002292f,
                    0.00000067f,  0.00002292f,  0.00019117f,  0.00038771f,  0.00019117f,  0.00002292f,  0.00000067f
                };
            int wIndex = 0;
			
            float2 offset = _BlurSize / _ScreenParams.xy;
             float3 sum = float3(0,0,0);

            float2 distortion = tex2D(_NoiseTex, i.uv).xy;

            //i.scrPos.xy +=  distortion * _NoiseSize;

            int size = 0;

            sum += tex2Dproj(_BackgroudTex, float4(i.scrPos.xy  , i.scrPos.z, i.scrPos.w)) ;


            for(int j= -size; j<= size; j++)
                for(int k= -size; k<= size; k++)
                {
                    sum += tex2Dproj(_BackgroudTex, float4(i.scrPos.xy + offset * float2(j, k), i.scrPos.z, i.scrPos.w)) * weight[wIndex];
                    wIndex += 1;
                }

            //--------- 边框 ------------
            float3 viewDir = normalize(i.worldPos - _WorldSpaceCameraPos);
            float edge2 = clamp(abs(dot(viewDir, normalize(i.normal))/0.7)-0.7,0,1);
            float edge1 = 1 - tex2D(_EdgeTex, i.uv).x;

            float phase0 = frac(_Time.y + 0.5) ;
            float phase1 = frac(_Time.y) ;

            float4 color0 = tex2D(_LightTex, i.uv + phase0 ) ;
            float4 color1 = tex2D(_LightTex, i.uv + phase1 );

            float flowLerp = (abs(phase0 - 0.5)) * 2;
            float4 lightColor = lerp(color0, color1, flowLerp);

            //fixed4 lightColor = tex2D(_LightTex, i.uv + _Time.y);
            
            return float4(distanceRele * float3(0,1,1),1 );

            //return fixed4(sum, 1.0) + (edge1) * lightColor;

            //return fixed4(sum, 1.0);
            //return tex2D(_LightTex, i.uv);

			//return lerp(fixed4(sum, 1.0), tex2D(_StrokeTex, i.uv), distanceRele) + (edge1) * lightColor;
		}


        // struct v2fDof {
        //     float4 pos : SV_POSITION;
        //     half4 uv : TEXCOORD0;
        // };

        // v2fDof vertDof(appdata_img v){
        //     v2fDof o;
        //     o.pos = UnityObjectToClipPos(v.vertex);
        //     o.uv.xy = v.texcoord;
        //     o.uv.zw = v.texcoord;

        //     #if UNITY_UV_STARTS_AT_TOP
        //     if (_MainTex_TexelSize.y < 0.0)
        //         o.uv.w =  1.0 - o.uv.w;
        //     #endif

        //     return o;
        // }

        // fixed4 fragDof(v2fDof i) : SV_Target{

        //     fixed4 blurCol = tex2D(_Dof, i.uv.zw);

        //     fixed4 originCol = tex2D(_MainTex, i.uv.xy);

        //     float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);

        //     depth = Linear01Depth(depth);

        //     fixed4 finalCol = depth > _Focus ? (lerp(blurCol, originCol, 1.0f-clamp((depth - _Focus) * _NearScale,0,1)  )) : blurCol;

        //     finalCol = depth < _Focus ? (lerp(blurCol, originCol, 1.0f-clamp((_Focus - depth) * _FarScale,0,1)  )) : blurCol;

        //     return finalCol;
        // }

        ENDCG

        //ZTest Always Cull Off ZWrite Off


        Pass{
            
			CGPROGRAM  
			#pragma vertex vertGrabBlur 
			#pragma fragment fragGrabBlur  
			
			ENDCG  
        }


        // Pass {
        //     CGPROGRAM
        //     #pragma vertex vertDof
        //     #pragma fragment fragDof

        //     ENDCG
        // }

    }
     FallBack Off
}
